import os
import re

# 分类字典
classification_dict = {
    "100": "故事",
    "101": "文化",
    "102": "娱乐",
    "103": "体育",
    "104": "财经",
    "106": "房产",
    "107": "汽车",
    "108": "教育",
    "109": "科技",
    "110": "军事",
    "112": "旅游",
    "113": "国际",
    "114": "股票",
    "115": "三农",
    "116": "游戏"
}

def process_records(file_path, dir_name):
    try:
        # 读取文件内容
        with open(file_path, 'r', encoding='utf-8') as file:
            lines = file.readlines()

        # 遍历文件内容中的每一行
        for line in lines:
            # 使用 '!' 作为分隔符来分割行
            parts = line.strip().split('_!_')

            # 从分割后的部分中提取分类和记录ID（用作文件名）
            category_code = parts[1]
            record_id = parts[0]  # 使用记录ID作为文件名
            category_name = dir_name + classification_dict.get(category_code)

            new = parts[3]+''+parts[4]
            # print(new)

            # 如果分类代码不在字典中，则跳过该记录
            if not category_name:
                print(f"Skipped line with unknown category code: {category_code}")
                continue

            # 如果目录不存在，则创建目录
            os.makedirs(category_name, exist_ok=True)

            # print("运行到这啦！！！")

            # 在分类目录中写入新文件，包含完整的记录内容
            with open(f"{category_name}/{record_id}.txt", 'w', encoding='utf-8') as new_file:
                new_file.write(new)

            #print("保存运行到这啦！！！")

    except FileNotFoundError:
        print(f"Error: The file at {file_path} does not exist.")
    except PermissionError:
        print(f"Error: Permission denied when accessing the file at {file_path}.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

# 确保这个文件存在
file_path = "../toutiao_cat_data.txt"
dir_name = "../toutiao_dataset/"

# 调用函数，传入文件路径
process_records(file_path,dir_name)